home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / audit.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  2KB  |  112 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4.  
  5. union header {
  6.     struct {
  7.         union header *ptr;
  8.         unsigned size;
  9.     } s;
  10.     long l;
  11. };
  12. /* Perform sanity checks on mbuf. Print any errors, return 0 if none,
  13.  * nonzero otherwise
  14.  */
  15. audit(bp,file,line)
  16. struct mbuf *bp;
  17. char *file;
  18. int line;
  19. {
  20.     register struct mbuf *bp1;
  21.  
  22.     for(bp1 = bp;bp1 != NULLBUF; bp1 = bp1->next)
  23.         audit_mbuf(bp1,file,line);
  24. }
  25. audit_mbuf(bp,file,line)
  26. register struct mbuf *bp;
  27. char *file;
  28. int line;
  29. {
  30.     union header *blk;
  31.     char *bufstart,*bufend;
  32.     int16 overhead = sizeof(union header) + sizeof(struct mbuf);
  33.     int16 datasize;
  34.     int errors = 0;
  35.     char *heapbot,*heaptop;
  36. #ifndef    ATARI_ST        /* Atari/MWC uses an arena, not a heap -- hyc */
  37.     extern char _Uend;
  38.     extern int _STKRED;
  39. #endif
  40.     if(bp == NULLBUF)
  41.         return;
  42.  
  43. #ifndef    ATARI_ST
  44.     heapbot = &_Uend;
  45.     heaptop = (char *) -_STKRED;
  46. #endif
  47.     /* Does buffer appear to be a valid malloc'ed block? */
  48.     blk = ((union header *)bp) - 1;
  49.     if(blk->s.ptr != blk){
  50.         printf("Garbage bp %lx\n",(long)bp);
  51.         errors++;
  52.     }
  53.     if((datasize = blk->s.size*sizeof(union header) - overhead) != 0){
  54.         /* mbuf has data area associated with it, verify that
  55.          * pointers are within it
  56.          */
  57.         bufstart = (char *)(bp + 1);
  58.         bufend = (char *)bufstart + datasize;
  59.         if(bp->data < bufstart){
  60.             printf("Data pointer before buffer\n");
  61.             errors++;
  62.         }
  63.         if(bp->data + bp->cnt > bufend){
  64.             printf("Data pointer + count past bounds\n");
  65.             errors++;
  66.         }
  67.     } else {
  68.         /* Dup'ed mbuf, at least check that pointers are within
  69.          * heap area
  70.         */
  71.  
  72.         if(bp->data < heapbot
  73.          || bp->data + bp->cnt > heaptop){
  74.             printf("Data outside heap\n");
  75.             errors++;
  76.         }
  77.     }
  78.     /* Now check link list pointers */
  79.     if(bp->next != NULLBUF && ((bp->next < (struct mbuf *)heapbot)
  80.          || bp->next > (struct mbuf *)heaptop)){
  81.             printf("next pointer out of limits\n");
  82.             errors++;
  83.     }
  84.     if(bp->anext != NULLBUF && ((bp->anext < (struct mbuf *)heapbot)
  85.          || bp->anext > (struct mbuf *)heaptop)){
  86.             printf("anext pointer out of limits\n");
  87.             errors++;
  88.     }
  89.     if(errors != 0){
  90.         dumpbuf(bp);
  91.         printf("PANIC: buffer audit failure in %s line %d\n",file,line);
  92.         fflush(stdout);
  93.         for(;;)
  94.             ;
  95.     }
  96.     return;
  97. }
  98. dumpbuf(bp)
  99. struct mbuf *bp;
  100. {
  101.     union header *blk;
  102.     if(bp == NULLBUF){
  103.         printf("NULL BUFFER\n");
  104.         return;
  105.     }
  106.     blk = ((union header *)bp) - 1;
  107.     printf("bp %lx tot siz %u data %lx cnt %u next %lx anext %lx\n",
  108.         (long)bp,blk->s.size * sizeof(union header),
  109.         (long)bp->data,bp->cnt,
  110.         (long)bp->next,(long)bp->anext);
  111. }
  112.